package com.jiukuaidao.zhulin.module.user;

import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.jiukuaidao.library.base.BaseFragment;
import com.jiukuaidao.library.util.ImageGlideUtils;
import com.jiukuaidao.library.util.ToastUtil;
import com.jiukuaidao.zhulin.R;
import com.jiukuaidao.zhulin.data.TaskProvider;
import com.jiukuaidao.zhulin.module.address.AddressManagerActivity;
import com.jiukuaidao.zhulin.module.order.OrderTabActivity;
import com.jiukuaidao.zhulin.module.order.RefundActivity;
import com.jiukuaidao.zhulin.module.user.contract.MineContract;
import com.jiukuaidao.zhulin.module.user.presenter.MinePresenter;
import com.jiukuaidao.zhulin.util.IntentUtils;
import com.jiukuaidao.zhulin.util.UserInfoUtil;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Author : PaiTaSuo
 * Date : 2016/5/25
 * Description : 我的
 */
public class MineFragment extends BaseFragment<MineContract.Prestener> implements MineContract.View, View.OnClickListener {

    @BindView(R.id.login_header)
    RelativeLayout mLoginHeader;
    @BindView(R.id.login_btn)
    TextView mLoginBtn;
    @BindView(R.id.iv_header)
    ImageView mIvHeader;
    @BindView(R.id.name_header)
    TextView mNameHeader;
    @BindView(R.id.noLogin_header)
    RelativeLayout mNoLoginHeader;
    @BindView(R.id.rl_address_manager)
    RelativeLayout mAddressManager;
    @BindView(R.id.rl_all_order)
    RelativeLayout mAllOrderRl;
    @BindView(R.id.rl_paid)
    RelativeLayout mRlPaid;
    @BindView(R.id.rl_receivables)
    RelativeLayout mRlReceivables;
    @BindView(R.id.rl_evaluate)
    RelativeLayout mRlEvaluate;
    @BindView(R.id.rl_refund)
    RelativeLayout mRlRefund;
    @BindView(R.id.mine_setting)
    ImageView mSettingIv;
    @BindView(R.id.rl_feed_back)
    RelativeLayout mRlFeedBack;
    @BindView(R.id.count_paid)
    TextView mCountPaid;
    @BindView(R.id.count_receivables)
    TextView mCountReceivables;
    @BindView(R.id.count_evaluate)
    TextView mCountEvaluate;
    @BindView(R.id.count_refund)
    TextView mCountRefund;

    @Override
    protected void init() {
        setOnClickListener();
        new MinePresenter(TaskProvider.provideTasksRepository(getActivity()), this);

    }

    @Override
    protected View getTargetView() {
        return null;
    }

    @Override
    protected void onUserVisible() {
        if (!UserInfoUtil.isLogin()) {
            mLoginHeader.setVisibility(View.GONE);
            mNoLoginHeader.setVisibility(View.VISIBLE);
            setCountTv(mCountPaid, 0);
            setCountTv(mCountReceivables, 0);
            setCountTv(mCountEvaluate, 0);
            setCountTv(mCountRefund, 0);
        }
        mPresenter.start();
    }

    @Override
    protected void onUserInvisible() {
    }

    @Override
    protected int getContentLayoutId() {
        return R.layout.fragment_mine;
    }


    @Override
    public void onClick(View view) {
        Bundle bundle = new Bundle();
        switch (view.getId()) {
            case R.id.login_btn:
                startLoginJump(UserInfoUtil.isLogin(), this.getClass(), this.getClass(), LoginActivity.class, bundle);
                break;
            case R.id.mine_setting:
                intentJump(SettingActivity.class);
                break;
            case R.id.iv_header:
                if (UserInfoUtil.getLoginInfo() != null) {
                    bundle.putString("phone", UserInfoUtil.getLoginInfo().mobile);
                    bundle.putString("headerIcon", UserInfoUtil.getLoginInfo().headerImage);
                    bundle.putInt("isSetpass", UserInfoUtil.getLoginInfo().isSetpass);
                    intentJump(UserInfoActivity.class, bundle);
                }
                break;
            case R.id.rl_address_manager:
                bundle.putString("fromActivity", "mineFragment");
                startLoginJump(UserInfoUtil.isLogin(), AddressManagerActivity.class, getActivity().getClass(), LoginActivity.class, bundle);
                break;
            case R.id.rl_all_order:
                bundle.putInt("type", 0);
                startLoginJump(UserInfoUtil.isLogin(), OrderTabActivity.class, getActivity().getClass(), LoginActivity.class, bundle);
                break;
            case R.id.rl_paid:
                bundle.putInt("type", 1);
                startLoginJump(UserInfoUtil.isLogin(), OrderTabActivity.class, getActivity().getClass(), LoginActivity.class, bundle);
                break;
            case R.id.rl_receivables:
                bundle.putInt("type", 2);
                startLoginJump(UserInfoUtil.isLogin(), OrderTabActivity.class, getActivity().getClass(), LoginActivity.class, bundle);
                break;
            case R.id.rl_evaluate:
                bundle.putInt("type", 3);
                startLoginJump(UserInfoUtil.isLogin(), OrderTabActivity.class, getActivity().getClass(), LoginActivity.class, bundle);
                break;
            case R.id.rl_refund:
                bundle.putInt("type", 4);
                startLoginJump(UserInfoUtil.isLogin(), RefundActivity.class, getActivity().getClass(), LoginActivity.class, bundle);
                break;
            case R.id.rl_feed_back:
                ToastUtil.show("开发中", Toast.LENGTH_SHORT);
                break;
        }
    }

    public void setOnClickListener() {
        mLoginBtn.setOnClickListener(this);
        mSettingIv.setOnClickListener(this);
        mAddressManager.setOnClickListener(this);
        mAllOrderRl.setOnClickListener(this);
        mRlPaid.setOnClickListener(this);
        mRlReceivables.setOnClickListener(this);
        mRlEvaluate.setOnClickListener(this);
        mRlFeedBack.setOnClickListener(this);
        mRlRefund.setOnClickListener(this);
        mIvHeader.setOnClickListener(this);
    }


    @Override
    public void refreshView() {
        if (UserInfoUtil.isLogin()) {
            mLoginHeader.setVisibility(View.VISIBLE);
            mNoLoginHeader.setVisibility(View.GONE);
            mNameHeader.setText(UserInfoUtil.getLoginInfo().mobile.substring(0,3) + "****" + UserInfoUtil.getLoginInfo().mobile.substring(7,11));
            ImageGlideUtils.CircleGlideImages(this, UserInfoUtil.getLoginInfo().headerImage, mIvHeader, R.mipmap.ic_mine_header);
            setCountTv(mCountPaid, UserInfoUtil.getLoginInfo().toBePaidCount);
            setCountTv(mCountReceivables, UserInfoUtil.getLoginInfo().toBeRecivedCount);
            setCountTv(mCountEvaluate, UserInfoUtil.getLoginInfo().toBeEvaluatedCount);
            setCountTv(mCountRefund, UserInfoUtil.getLoginInfo().refundsCount);
        } else {
            mLoginHeader.setVisibility(View.GONE);
            mNoLoginHeader.setVisibility(View.VISIBLE);
        }
    }

    public void setCountTv(TextView view, int count) {
        if (count == 0) {
            view.setVisibility(View.GONE);
        } else {
            view.setVisibility(View.VISIBLE);
            view.setText(String.valueOf(count));
        }
    }

    @Override
    public void toast(String msg) {
        ToastUtil.show(msg, Toast.LENGTH_SHORT);
    }

    @OnClick(R.id.rl_service_tel)
    public void setCallTel() {
        IntentUtils.callPhone(getActivity(), "400-616-9999");
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
            case IntentUtils.REQUEST_CODE_CALLPHONE:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
                    IntentUtils.realCallPhone(getActivity(), "400-616-9999");
                break;
        }
    }
}
